package com.gtq.uhome.service.serviceImpl;

import cn.hutool.core.collection.CollectionUtil;
import com.gtq.uhome.mapper.*;
import com.gtq.uhome.pojo.Site;
import com.gtq.uhome.pojo.StaffRole;
import com.gtq.uhome.pojo.StaffSite;
import com.gtq.uhome.result.BusinessException;
import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.SiteService;
import com.gtq.uhome.util.MD5Creater;
import com.gtq.uhome.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * @description:
 * @author: Zhong Ranjie
 * @create: 2020-08-04 13:43
 **/
@Service

public class SiteServiceImpl implements SiteService {

    @Autowired
    private SiteMapper siteMapper;
    @Autowired
    private StaffRoleMapper staffRoleMapper;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private StaffSiteMapper staffSiteMapper;

    @Override
    public Result addNewSite(SiteVo site) {
        site.setStatusCode("0");
        Site siteByName = siteMapper.findSiteByName(site.getSiteName());
        if(Objects.nonNull(siteByName)){
            return Result.failed("站点名重复");
        }
        boolean b = siteMapper.addNewSite(site);
        if(!b){
            return Result.failed("站点添加失败");
        }
        return Result.ok("添加成功");
    }

    @Override
    public List<Site> searchSite(String siteName) {
        return siteMapper.searchSite(siteName);
    }

    private boolean addSiteRelation(AddSiteMemberVo siteMemberVo) {
        siteMemberVo.setAddTime(LocalDateTime.now());
        //通过电话找到员工信息
        StaffVo staffVo = staffMapper.findByPhone(siteMemberVo.getPhone());
        if (Objects.nonNull(staffVo)) {
            siteMemberVo.setStaffId(staffVo.getStaffId());
            StaffSite relation = staffSiteMapper.findRelation(staffVo.getStaffId());
            if (Objects.isNull(relation)) {
                boolean b1 = staffSiteMapper.addSiteMember(siteMemberVo);
                if (b1) {
                    return this.addStaffRole(staffVo.getStaffId(), siteMemberVo);
                }
            }
        }
        return false;
    }

    private boolean addStaffRole(Integer staffId, AddSiteMemberVo siteMemberVo) {
        StaffRole staffRole = staffRoleMapper.selectByStaffId(staffId);
        if (Objects.isNull(staffRole)) {
            return staffRoleMapper.addSiteMember(siteMemberVo);
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addSiteMember(AddSiteMemberVo siteMemberVo) {
        Site site = siteMapper.findSiteByName(siteMemberVo.getSiteName());
        if(Objects.isNull(site)){
            throw new BusinessException("站点不存在");
        }
        siteMemberVo.setSiteId(site.getSiteId());
        int roleId = roleMapper.findRoleId("站点员");
        siteMemberVo.setPassword(MD5Creater.encode("123456" + siteMemberVo.getPhone()));
        siteMemberVo.setStatusCode("0");
        siteMemberVo.setFreeze("正常");
        siteMemberVo.setRoleId(roleId);
        //通过电话查询员工是否已存在
        StaffVo staffVo = staffMapper.findByPhone(siteMemberVo.getPhone());
        if(Objects.nonNull(staffVo)){
            return Result.failed("添加失败，电话重复");
        }
        //不存在员工信息 新增
        boolean b = staffMapper.addSiteMember(siteMemberVo);
        if (b) {
            //添加关联
            boolean b1 = addSiteRelation(siteMemberVo);
            if(b1){
                return Result.ok("添加成功");
            }
            return Result.failed("添加关联失败");
        }
        return Result.failed("添加失败");
    }

    @Override
    public StaffVo findByPhone(String phone) {
        StaffVo staffVo = new StaffVo();
        staffVo.setPhone(phone);
        return staffMapper.findByPhone(phone);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSiteMember(SiteMemberUpdateVo siteMemberVo) {
        //修改站点员信息
        if (Objects.isNull(siteMemberVo.getName()) || Objects.isNull(siteMemberVo.getPhone())) {
            throw new BusinessException("姓名和电话不能为空");
        }
        boolean b = staffMapper.updateSiteMember(siteMemberVo);
        if (b) {
            AddSiteMemberVo vo = new AddSiteMemberVo();
            if (Objects.isNull(siteMemberVo.getSiteId()) || Objects.isNull(siteMemberVo.getStaffId())) {
                throw new BusinessException("信息不全");
            }
            vo.setStaffId(siteMemberVo.getStaffId());
            vo.setSiteId(siteMemberVo.getSiteId());
            vo.setStatusCode("0");
            //修改站点关联
            return staffSiteMapper.updateSiteMember(vo);
        }
        throw BusinessException.resourceUpdateException();
    }

    @Override
    public List<SiteMemberVo> findSiteMembers(Integer siteId, String memberName) {
        //找到员工和站点关联
        List<StaffSite> staffSiteList = staffSiteMapper.findSiteMember(siteId);
        List<SiteMemberVo> voList = new ArrayList<>();
        //将结果添加到返回的list
        if (CollectionUtil.isNotEmpty(staffSiteList)) {
            staffSiteList.forEach(staffSite -> {
                if (Objects.nonNull(staffSite)) {
                    //查询员工信息(模糊查询）
                    StaffVo staffVo = staffMapper.findById(staffSite.getStaffId(), memberName);
                    //查询是否为站点员
                    StaffRole staffRole = staffRoleMapper.selectByStaffId(staffSite.getStaffId());
                    if (Objects.nonNull(staffVo) && Objects.nonNull(staffRole)) {
                        SiteMemberVo memberVo = new SiteMemberVo();
                        memberVo.setStaffId(staffVo.getStaffId());
                        memberVo.setSiteId(staffSite.getSiteId());
                        memberVo.setName(staffVo.getStaffName());
                        memberVo.setPhone(staffVo.getPhone());
                        memberVo.setAddTime(staffSite.getAddTime());
                        Site site = siteMapper.findSite(siteId);
                        if (Objects.nonNull(site)) {
                            memberVo.setSite(site.getSiteName());
                            voList.add(memberVo);
                        }
                    }
                }
            });
        }
        return voList;
    }

    @Override
    public boolean updateSite(SiteUpdateVo updateVo) {
        updateVo.setStatusCode("0");
        return siteMapper.updateSite(updateVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSiteMember(List<Integer> staffIdList) {
        boolean flag = false;
        for (Integer staffId : staffIdList) {
            StaffRole staffRole = staffRoleMapper.selectByStaffId(staffId);
            StaffSite relation = staffSiteMapper.findRelation(staffId);
            if (Objects.nonNull(staffRole) && Objects.nonNull(relation)) {
                boolean deleteStaffRole = staffRoleMapper.deleteStaffRole(staffId);
                boolean deleteRelation = staffSiteMapper.deleteRelation(staffId);
                flag = deleteStaffRole && deleteRelation;
            }
            if (!flag) {
                break;
            }
        }
        return flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSiteList(List<Integer> idList) {
        boolean b = false;
        for (Integer id : idList) {
            Site site = siteMapper.findSite(id);
            if (Objects.nonNull(site)) {
                b = siteMapper.deleteSite(id);
            }
            if (!b) {
                break;
            }
        }
        return b;
    }
}
